package event;

import java.io.*;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.Hashtable;
import utility.*;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class EventHandler extends Thread
{
	String path = Input.PROCESSLOGGERPATH;
	//String masterPath = Input.MASTERREPORTSPATH;
	
	//String fileFormat = ".txt";
	String fileName;
	RandomAccessFile rwFile;

	final String MSG_ID = "msgid";	
	final String REQUEST = "request";
	final String PROCESS = "process";
	final String STATUS = "status";
	final String SCHEDULEDATE="scheduledate";
	final String MASTEROBJECT = "masterobject";
	final String USERNAME = "username";
	final String TYPE="mastertype";
	final String PRODUCT="product";	
	
	DBManager db ;
	GetIdName gin;
	final String tablename= "eventprocess";
	ResultSet resultset;
	
	public EventHandler() 
	{	
		db = new DBManager();
		db.getConnect();
		gin = new GetIdName(db);
	}
	public void run()
	{
		db = new DBManager();
		db.getConnect();
		gin = new GetIdName(db);
		
		while(true)
		{	
			try 
			{
				initialize();
				sleep(2000);
			} catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
	}
	public void initialize()
	{
		try
		{
			Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
			Condition.put("status", 0);			
			resultset = db.getselect(Condition, tablename);
			getEvent();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	void getEvent() 
	{
		try 
		{
			String line ="";
			while(resultset.next())
			{
				try
				{
				
					System.out.println("event::EventHandler::getEvent:: The Line is "+line);					
					String Trans_type = resultset.getString(TYPE);
					String event_id = resultset.getString(MASTEROBJECT);
							
					System.out.println("event::EventHandler::getEvent:: The Line is "+line);
					Hashtable<Object,Object> temp = new Hashtable<Object, Object>();
					RecordSearch rs = new RecordSearch(db);
					temp.put("td", "null");
					temp.put("mid", event_id);			
					//String request[] = rs.getArrayFromResultSet();
					String Process=resultset.getString(PROCESS);
					//String process[] = rs.getArrayFromResultSet();
				//	if(process.length>0 && request.length>0)
					{
							temp.put("pid",gin.getId("type"));
							int pv_index = Column.pv_index-1;
							int vt_index = Column.vt_index-1;
							rs.setConditionMap(temp);
							String [] getEvent = rs.getArrayFromResultSet()[0].split("\t");
							String eventValue = getEvent[pv_index];
							if(eventValue.equals("0"))
								eventValue = getEvent[vt_index];
							else
								eventValue = gin.getItem(eventValue);						
							Event startEvent;
							System.out.println("event::EventHandler::getEvent:: Start Event "+eventValue);
							String msgid=resultset.getString(MSG_ID);
							Class C = Class.forName("event."+CapitalChar.makeFirstCharCapital(eventValue));
							startEvent = (Event)C.newInstance();
							startEvent.SetData(resultset.getString(PRODUCT), Trans_type,msgid, resultset.getString(USERNAME),Process);
							startEvent.initializationData(db);
							startEvent.setObject(event_id);							
							String Methodname = "after"+CapitalChar.makeFirstCharCapital(Process);
							Method method = C.getDeclaredMethod(Methodname,new Class[0]);
							Boolean result = (Boolean) method.invoke(startEvent, new Object[0]);							
							if(result.booleanValue()==true)
							{	
								db.getUpdate("update eventprocess set status=5 where msgid='"+msgid+"'");
							}	
					}		
				}
				catch (Exception e) 
				{
					e.printStackTrace();
					System.out.println(e.getMessage());
				}
					}
				}
				catch (Exception e) 
				{
				}
	}
	
	void startEvent(String Event)
	{
		System.out.println("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
	
	}
	public static void main(String []a)
	{
		EventHandler eHandler = new EventHandler();
		eHandler.initialize();
		
	}
}
